package com.wy.web.old_contact;

import com.wy.service.ContactService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/contact/deleteContactById")
public class DeleteContactByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求携带的id值
        String id = req.getParameter("id");
        //2.调用service处理业务
        ContactService service = new ContactService();
        boolean flag = service.deleteContactById(id);
        //3.判断执行结果,生成响应信息
        if(flag){
            //重定向查询所有联系人信息
            resp.sendRedirect(req.getContextPath()+"/contact/findAll");
            System.out.println(req.getContextPath()+"/contact/findAll");
        }else{
            //重定向到错误页面进行委婉提示
            resp.sendRedirect(req.getContextPath()+"/error.jsp");

        }
    }
}
